#    Supsta
#    Copyright (c) 2014 Sebastian Dufner
#
#    This file is part of Supsta.
#
#    Supsta is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Supsta is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

##

module Supsta
  module Gimp

    ##
    # A Scope.
    #
    # *Note:* This module depends on the following methods:
    # [scope]       The parent Scope.
    # [name]        This scope's name (String).
    # [script]      The Scheme source code generated by this scope (doesn't
    #               have to be well-formed).
    # [nvars]       The number of variables (can be private).
    # [nvars=]      Setter for nvars (can be private).
    # [def]         Define a variable in the current scope.

    module Scope

      ##
      # Returns a new unique variable name.

      def new_var
        "#{name}var#{nvars += 1}"
      end

      ##
      # Returns a String containing a scope prefix.

      def new_scope
        "#{name}scope#{nvars += 1}"
      end

      ##
      # Instructs GIMP to issue a message.

      def message(*args)
        s = ''
        args.each do |arg|
          s.concat '(gimp-message '
          s.concat "\"#{arg.to_s.gsub(/\\/, '\\\\').gsub(/"/, '\"')}\")\n"
        end

        @script.concat s

        self
      end
      alias_method :puts, :message

      ##
      # Loads +file+ and returns the Image.

      def load_image(file)
        image = "image#{@nvars += 1}"
        file = file.gsub(/\\/, '\\\\').gsub(/"/, '\"')

        self.def image, "(car(gimp-file-load RUN-NONINTERACTIVE \"#{file}\" \"#{file}\"))"

        Image.new(self, image).set_profile_rgb
      end

      ##
      # Returns a new Image with dimensions +width+ and +height+.

      def new_image(width, height)
        image = "image#{nvars += 1}"
        fail RangeError, "image width out of range: #{width}" unless
          (1..262144).include? width
        fail RangeError, "image height out of range: #{height}" unless
          (1..262144).include? height

        self.def image, "(car(gimp-image-new #{width} #{height} 0))"
        s = "(gimp-image-insert-layer #{image} (car(gimp-layer-new "
        s.concat "#{image} #{width} #{height} 1 \"background\" 100 0)) 0 -1"
        s.concat ")\n"
        script.concat s

        Image.new(self, image).set_profile_rgb
      end

      ##
      # Sets the foreground color to +r+, +g+, +b+.

      def set_foreground(r, g, b)
        @script.concat "(gimp-context-set-foreground '(#{r} #{g} #{b}))"

        self
      end

    end
  end
end
